/**
 * 
 */
package info.niwota.web.proxy;

import jvs.peer.io.ChannelManager;
import jvs.peer.util.Log;

import org.apache.commons.pool.KeyedPoolableObjectFactory;

/**
 * @author qiangli
 * 
 */
public class ServerSocketThreadObjectFactory implements
		KeyedPoolableObjectFactory {
	private static final String TAG = "ServerSocketThreadObjectFactory";
	
	private String realm = ChannelManager.NETWORK_BTSPP;
	
	public ServerSocketThreadObjectFactory(String realm) {
		this.realm = realm;
	}
	
	public Object makeObject(Object key) throws Exception {
		if (Log.DEBUG) {
			Log.d(TAG, "makeObject: " + key);
		}

		ServerSocketThread t = new ServerSocketThread(realm, key.toString());
		t.setDaemon(true);
		t.start();
		return t;
	}

	public void destroyObject(Object key, Object obj) throws Exception {
		if (Log.DEBUG) {
			Log.d(TAG, "destroyObject: " + key + ":" + obj);
		}
		
		ServerSocketThread t = (ServerSocketThread) obj;
		t.cancel();
	}

	public boolean validateObject(Object key, Object obj) {
		if (Log.DEBUG) {
			Log.d(TAG, "validateObject: " + key + ":" + obj);
		}
		
		ServerSocketThread t = (ServerSocketThread) obj;
		
		return t.isRunning();
	}

	public void activateObject(Object key, Object obj) throws Exception {
		if (Log.DEBUG) {
			Log.d(TAG, "activateObject: " + key + ":" + obj);
		}
	}

	public void passivateObject(Object key, Object obj) throws Exception {
		if (Log.DEBUG) {
			Log.d(TAG, "passivateObject: " + key + ":" + obj);
		}
	}
}
